<script type="text/javascript">
ResetGAC();
iseInitUpdateArrays();

<%
  system.SetSessionVar("sessionTAB", "statusviews/alarmMessages");
  
  boolean firstSort = false;
  object oUser = dom.GetObject( system.GetSessionVar("sessionUserID") );
  if( oUser && (system.GetSessionVar("sessionCTV")=="true") )
  {
    oUser.UserTempViewIDs().RemoveAll();
    
    object oTmpArray = dom.GetObject( ID_SYSTEM_VARIABLES );
    if( oTmpArray )
    {
      string sTmp;
      foreach(sTmp,oTmpArray.EnumIDs())
      {
        object oTmp = dom.GetObject( sTmp );
        if( oTmp )
        {
          if( oTmp.IsTypeOf( OT_ALARMDP ) && ( oTmp.AlState() == asOncoming ) )
          {
            oUser.UserTempViewIDs().Add( oTmp.ID() );
          }
        }
      }
    }
    firstSort=true;
  }
%>
  setPath(translateKey("alarmMsg"));
  fltAL = new iseFilter("fltAL");
  arAlarms = new Array();
</script>
<div id="dummy"></div>
<div id="NoAlarms" class="CLASS02800" style="display:none;">${noAlarmMessagePresent}</div>
<script type="text/javascript">
  Cursor.set(Cursor.NORMAL);
</script>
<table id="tblAlarms" class="filterTable tTable CLASS02601" border="0" cellpadding="0" cellspacing="0" style="display:none;">
<colgroup>
  <col style="width:100px;" />
  <col style="width:100px;" />
  <col style="width:50px;" />
  <col />
  <col />
  <col />
  <col />
  <col />
  <col style="width:100px;" />
</colgroup>
<tr>
  <%string sCssName="Sort"; if(system.GetSessionVar("sessionLS")=="first"){sCssName="SortSelected";} %>
  <th align="center" class="thCell CLASS02803"><div id="first" class="pt11 <%Write(sCssName);%>" onclick="IseSort(arAlarms,'first',true);">${thFirstTriggerDateTime}</div></th>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 CLASS02815">${thLastTriggerDateTime}</div></th>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 CLASS02815">${thNumberOfTriggerEvents}</div></th>
  <%string sCssName="Sort"; if(system.GetSessionVar("sessionLS")=="name"){sCssName="SortSelected";} %>
  <th align="center" class="thCell CLASS02803"><div class="pt11 <%Write(sCssName);%>" onclick="IseSort(arAlarms,'name',true);">${thNameOfAlarm}</div></th>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 CLASS02815">${thState}</div></th>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 CLASS02815">${thDescription}</div></th>
  <%string sCssName="Sort"; if(system.GetSessionVar("sessionLS")=="message"){sCssName="SortSelected";} %>
  <th align="center" class="thCell CLASS02803"><div class="pt11 <%Write(sCssName);%>" onclick="IseSort(arAlarms,'message',true);">${thAlarmMessage}</div></th>
  <%string sCssName="Sort"; if(system.GetSessionVar("sessionLS")=="room"){sCssName="SortSelected";} %>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 <%Write(sCssName);%>" onclick="IseSort(arAlarms,'room',true);">${thRoom}</div></th>
  <th align="center" class="thCell CLASS02803" ><div class="pt11 CLASS02815">${thAction}</div></th>
</tr>
<tr>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804">
  <div id="btnFilterName" class="FilterBtn">${thFilter}</div>
    <div class="FilterSubMenu" id="btnFilterNameSub" style="display:none">
        <input id="ftName" type="text" size="15" class="CLASS02811" />
        <div class='FilterSetButton CLASS02812' onclick="setSFilter(fltAL, 'ftName', FIL_NAME);updateTable();$('btnFilterNameSub').hide();" >${filterSet}</div>
        <div class='FilterSetButton' onclick="$('btnFilterNameSub').hide();">${filterClose}</div>
    </div>
  </td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804">
    <div id="btnFilterDesc" class="FilterBtn">${thFilter}</div>
    <div class="FilterSubMenu" id="btnFilterDescSub" style="display:none">
        <input id="ftDesc" type="text" size="15" class="CLASS02811" />
        <div class="FilterSetButton CLASS02812" onclick="setSFilter(fltAL, 'ftDesc', FIL_DESC);updateTable();$('btnFilterDescSub').hide();" >${filterSet}</div>
        <div class='FilterSetButton' onclick="$('btnFilterDescSub').hide();">${filterClose}</div>
    </div>
  </td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804">
    <div id="btnFilterRoom" class="FilterBtn">${thFilter}</div>
    <div class="FilterSubMenu" id="btnFilterRoomSub" style="display:none">
      <% 
        Write("<table class='CLASS02813'>");
        object obj = dom.GetObject(ID_ROOMS);
        if ( obj ) {
          string tmp = "";
          foreach( tmp, obj.EnumEnabledIDs() ) {
            object elem = dom.GetObject(tmp);
            if (elem) {
              string divId = "rdiv"#tmp;
                Write("<tr>");
                  Write("<td  class='CLASS02814'>"#elem.Name()#"</td>");
                  Write("<td  class='CLASS02814'><input type='checkbox' id='vf"#elem.ID()#"'/></td>");
                Write("</tr>");
            }
          }
        }
        Write("</table>");
        Write("<div class='FilterSetButton' onclick='setAFilter(fltAL, \"rooms\");updateTable();$(\"btnFilterRoomSub\").hide();'>${filterSet}</div>");
        Write("<div class='FilterSetButton' onclick='$(\"btnFilterRoomSub\").hide();'>${filterClose}</div>");
      %>
    </div>
  </td>
  <td style="text-align:center; vertical-align: middle;" class="CLASS02804"></td>
</tr>
<%
  integer iAlCount = 0;

  string sEnumUserIDs = oUser.UserTempViewIDs().EnumIDs();
  string sEnumUserInternalIDs = oUser.UserTempViewIDs().EnumEnabledInternalIDs();
  if( sEnumUserInternalIDs != "" ) {
    if( sEnumUserIDs != "" ) {
      sEnumUserIDs = sEnumUserIDs # "\t" # sEnumUserInternalIDs;
    } else {
      sEnumUserIDs = sEnumUserInternalIDs;
    }
  }

  string sSVID;
  foreach( sSVID, sEnumUserIDs )
  {
    object oSV = dom.GetObject( sSVID );
    if( oSV )
    {
      iAlCount = iAlCount + 1;
      string sTimestamp = oSV.AlOccurrenceTime();
      Call( "/esp/TimeStamp.fn::TimeStampToString()" );
      string sFirstTimestamp = sTimestamp;
      string sTimestamp = oSV.Timestamp();
      Call( "/esp/TimeStamp.fn::TimeStampToString()" );
      string sLastTimestamp = sTimestamp;
     
      object oDestDP = dom.GetObject( oSV.AlDestMapDP() );
      string sDestDPName = "";
      if( oDestDP )
      {
        sDestDPName = '<a href="#" class="CLASS02805">&lt;'#oDestDP.Name()#'&gt;</a>';
      }
      else
      {
        sDestDPName = "no data point";
      }
      
      string sAlarmName = oSV.Name();
      if( !sAlarmName.Length() )
      {
        sAlarmName = "no name";
      }
      
      string sAlarmDescription = oSV.DPInfo().Replace('\r\n', '<br/>').Replace('\r', '<br/>').Replace('\n', '<br/>');
      if( !sAlarmDescription.Length() )
      {
        sAlarmDescription = "no description";
      }
      
      string sRoom = "";
      string sLastTriggerName = "";
      string sLastTriggerMessage = "";
      string sLastTriggerKey = "";
      integer iTmpTriggerID = oSV.LastTriggerID();
      if( iTmpTriggerID == ID_ERROR )
      {
        iTmpTriggerID = oSV.AlTriggerDP();
      }

      string sAlarmMessage = "";
      string sChannelName = "";
      string sRooms = "";
      string roomIDs = "";
      object oLastTrigger = dom.GetObject( iTmpTriggerID );
      if( oLastTrigger )
      {           
        object oLastTriggerChannel = dom.GetObject( oLastTrigger.Channel() );
        if( oLastTriggerChannel )
        {
          sChannelName = oLastTriggerChannel.Name();
          string sLastTriggerName = sChannelName;
          string sRID;
          foreach( sRID, oLastTriggerChannel.ChnRoom() )
          {
            object oRoom = dom.GetObject( sRID );
            if( oRoom )
            {
              sRooms = sRooms#"<br />"#oRoom.Name();
              roomIDs = roomIDs#sRID# "\t";
            }
          }
          
          if( oLastTrigger.IsTypeOf( OT_HSSDP ) )
          {
            string sLongKey = oLastTriggerChannel.ChnLabel()#"|"#oLastTrigger.HSSID();
            string sShortKey = oLastTrigger.HSSID();
            if( (oLastTrigger.ValueType() == ivtInteger) && (oLastTrigger.ValueSubType() == istEnum) )
            {
              sLongKey = sLongKey#"="#web.webGetValueFromList( oLastTrigger.ValueList(), oSV.Value() );
              sShortKey = sShortKey#"="#web.webGetValueFromList( oLastTrigger.ValueList(), oSV.Value() );
            }
            ! sAlarmMessage = web.webKeyFromStringTable( sLongKey );

            if ((oSV.Value() == false) || (oSV.Value() == "")) {
              sAlarmMessage = oSV.ValueName0();
            } else {
              sAlarmMessage = oSV.ValueName1();
            }

            if( !sAlarmMessage.Length() )
            {
              sAlarmMessage = web.webKeyFromStringTable( sShortKey );
              if( !sAlarmMessage.Length() )
              {
                sAlarmMessage = "${unknownMessage} ("#sShortKey#")";
              }
            }
          }
        }
      }
      else
      {
        if (oSV.IsTypeOf(OT_ALARMDP)) 
        {
          if ((oSV.Value() == false) || (oSV.Value() == "")) {
            sAlarmMessage = oSV.ValueName0();
          }
          else {
            sAlarmMessage = oSV.ValueName1();
          }
        }
      }
      
      string sTrId = "al"#oSV.ID();
      Write( '<tr id="'#sTrId#'">' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02806">'#sFirstTimestamp#'</td>' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02806">'#sLastTimestamp#'</td>' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02807">'#oSV.AlCounter()#'</td>' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02807">'#sAlarmName#'</td>' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02807">${triggered}</td>' );
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02806">'#sAlarmDescription#'</td>' );
        if (sLastTriggerName != "")
        {
          sLastTriggerName = "<p class='CLASS02808'>"#sLastTriggerName#"</p>";
        }
        Write( '<td style="text-align:center; vertical-align: middle;"class="CLASS02806">'#sLastTriggerName#'<p>'#sAlarmMessage#'</p></td>' );
        Write( '<td style="text-align:center; vertical-align: middle;"class="CLASS02807">'#sRooms#'</td>' );

        ! The value of this button is set by JavaScript at the end of this page >> translateButton("btnConfirm");
        Write( '<td style="text-align:center; vertical-align: middle;" class="CLASS02807"><input type="button" name="btnConfirm" value="btnConfirm" onclick="ReceiptAlarm('#oSV.ID()#');" class="CLASS02809 colorGradient" /></td>' );
      Write( '</tr>' );
      
      WriteLine( '<script type="text/javascript">' );
      WriteLine( 'var tmp = new Object();' );
      WriteLine( 'tmp["id"] = '#oSV.ID()#'; ' );
      WriteLine( 'tmp["trid"] = "'#sTrId#'"; ' );
      WriteLine( 'tmp["first"] = "'#sFirstTimestamp#'"; ' );
      WriteLine( 'tmp["name"] = "'#oSV.Name()#'"; ' );
      WriteLine( 'tmp["message"] = "'#sLastTriggerName#'"; ' );
      WriteLine( 'tmp["room"] = "'#sRoom#'"; ' );
      WriteLine( 'tmp["fltOpts"] = new Object();' );
      WriteLine( 'tmp["fltOpts"]["rooms"] = "'#roomIDs#'";' );
      WriteLine( 'arAlarms[arAlarms.length] = tmp;' );
      WriteLine( 'Cursor.set(Cursor.NORMAL);' );
      WriteLine( '</script>' );
    }
  }
%>
</table>

<script type="text/javascript">
  
  <%
    if(firstSort)
    {
      Write("//IseSort(arAlarms,'first',true);");
    }
  %>
  
  if ( <% Write(iAlCount); %> == 0)
  {
    show("NoAlarms");
  }
  else
  {
    show("tblAlarms");
  }
  
  var mouseOpts = 
  {
    onTopOver: function(divToShow)
    {
      selectFilters(fltAL, divToShow);
    }
  };
  
  new iseSubMenuControl("btnFilterName", "btnFilterNameSub", "tblAlarms", {r: 11, d:22}, mouseOpts, false, true);
  new iseSubMenuControl("btnFilterRoom", "btnFilterRoomSub", "tblAlarms", {r: 11, d:22}, mouseOpts, false, true);
  new iseSubMenuControl("btnFilterDesc", "btnFilterDescSub", "tblAlarms", {r: 11, d:22}, mouseOpts, false, true);
  
  _ReceiptAlarm = function(id)
  {
    var url = '/esp/functions.htm?sid='+SessionId;
    var pb = "";
    pb += 'string action = "ReceiptAlarm";';
    pb += 'string id = "'+id+'";';
    var opt =  {
      postBody: pb,
      onComplete: function(t)
      {
        if(dbg)alert(t.responseText);
        reloadPage();
      }
    };
    new Ajax.Request(url,opt);
  };

  var s = "";
  
  s += "<table cellspacing='8'>";
  s += "<tr>";
    s += "<td  style='text-align:center; vertical-align: middle;'><div class='FooterButton' onclick='WebUI.goBack();'>"+ translateKey('footerBtnPageBack') +"</div></td>";
    s += "<td  style='text-align:center; vertical-align: middle;'><div class='FooterButton CLASS02810' onclick='fltAL.clearFilters();updateTable();'>"+ translateKey('footerBtnResetFilter') +"</div></td>";
    s += "<td  style='text-align:center; vertical-align: middle;'><div class='FooterButton CLASS02810' onclick='ackAll();'>"+ translateKey('footerBtnConfirmAll') +"</div></td>";
  s += "</tr>";
  s += "</table>";
  
  setFooter(s);
  
  ackAll = function()
  {
    for (var i = 0; i < arAlarms.length; i++)
    {
      ReceiptAlarm(arAlarms[i]['id']);
    }
  };
  
  updateTable = function()
  {
    for (var i = 0; i < arAlarms.length; i++)
    {
      if (!fltAL.objPassFilter(arAlarms[i]))
      {
        hide(arAlarms[i]['trid']);
      }
      else
      {
        show(arAlarms[i]['trid']);
      }
    } 
  };
  translatePage("#tblAlarms, #NoAlarms");
  translateButtons("btnConfirm");
</script>
  
